package com.test.controller;

import com.github.pagehelper.PageInfo;
import com.test.entity.Category;
import com.test.entity.Goods;
import com.test.service.CategoryService;
import com.test.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/goods")
@Api("商品管理")
public class GoodsCtrl {

    @Resource
    GoodsService goodsService;

    @Resource
    CategoryService categoryService;

    @RequestMapping(value = "/getGoodsList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation("获取商品")
    public String getGoodsList(Model model,
                               @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<Goods> goodsListPage = goodsService.queryAllByPage(pageNum, pageSize);
//        List<Goods> goodsList = goodsService.queryAllGoods();
        model.addAttribute("goodsListPage", goodsListPage);
        return "goodsList";
    }

    @GetMapping("delGoods/{id}")
    @ApiOperation("删除商品")
    public String delGoods(@PathVariable("id") int gdId, Model model) {
        System.out.println("这是get请求");
        boolean flag = goodsService.deleteById(gdId);
        if (flag) {
            model.addAttribute("msg", "商品删除成功");
        } else {
            model.addAttribute("msg", "商品删除失败");
        }
        return "forward:/admin/goods/getGoodsList";
    }

    @ResponseBody
    @DeleteMapping("/delGoods/{id}")
    public boolean delGoods2(@PathVariable("id") int gdID) {
        System.out.println("这是delete请求");
        boolean flag = goodsService.deleteById(gdID);
        return flag;
    }

    @GetMapping("/gotoAddGoods")
    @ApiOperation("跳转到添加商品")
    public String gotoAdd(Model model) {
        List<Category> categoryList = categoryService.queryAllCategory();
        model.addAttribute("categoryList", categoryList);
        return "goodsAdd";
    }

    @PostMapping("/addGoods")
    @ApiOperation("添加商品")
    public String addGoods(Goods goods, Model model) {
        model.addAttribute("msg", goodsService.insert(goods).getMessage());

        List<Category> categoryList = categoryService.queryAllCategory();
        model.addAttribute("categoryList", categoryList);

        return "goodsAdd";
    }


    @GetMapping("/{id}")
    @ApiOperation("商品详细信息")
    public String editGood(@PathVariable("id") int gId, Model model) {

        List<Category> categoryList = categoryService.queryAllCategory();
        model.addAttribute("categoryList", categoryList);

        // 根据ID获取商品信息
        Goods goods = goodsService.queryById(gId).getData();
        model.addAttribute("goods", goods);

        return "goodsEdit";
    }

    @PostMapping("editGoods")
    public String updateGoods(Goods goods, Model model) {
        model.addAttribute("msg", goodsService.update(goods).getMessage());
        List<Category> categoryList = categoryService.queryAllCategory();
        model.addAttribute("categoryList", categoryList);

        return "goodsEdit";
    }
}
